package com.atnjupt.web;

import com.atnjupt.bean.Book;
import com.atnjupt.bean.Page;
import com.atnjupt.service.impl.BookServiceImpl;
import com.atnjupt.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Wizarder
 * @create 2021-06-04 20:49
 */
public class BookServlet extends BaseServlet{
    private BookServiceImpl bookService = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1把提交的表单封装成book对象
        //pageNo是pageTotal
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        int pageTotalCount = bookService.pageTotalCount();
        if((pageTotalCount % pageNo)==0){
            pageNo++;
        }
        Book book = WebUtils.copyToBean(request.getParameterMap(), new Book());
        //2把该对象添加到数据库中
        bookService.addBook(book);
        //3请求重定向！！！不能用请求转发，因为最后一次是添加操作，F5刷新会一直重复这个请求
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);
    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取页面传过来的id参数
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //2、调用bookService方法删除该id对应的book对象
        bookService.deleteBookById(id);
        //3、请求重定向到/manager/bookServlet?action=list
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取页面参数，封装成Book对象
        Book book = WebUtils.copyToBean(request.getParameterMap(), new Book());
        //2、调用bookService中的updateBook方法更新
        bookService.updateBook(book);
        //3、请求重定向/工程名/manager/bookservlet
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1通过service模块查询所有书籍
        List<Book> books = bookService.queryBooks();
        //2把book对象封装到域对象中
        request.setAttribute("books",books);
        //3请求转发
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);
    }
    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取请求的参数
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //2、调用bookServlet查询对应id的book对象
        Book book = bookService.queryBookById(id);
        //3、把book对象存入request域数据中
        request.setAttribute("book",book);
        //4、请求转发到/pages/manager/book_edit.jsp
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //1、获取请求的参数pageNo，pageSize。首次访问pageNo=1,pageSize采用默认值
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIEZ);
        //2、获取page对象
        Page<Book> page = bookService.page(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3、保存到域数据
        request.setAttribute("page",page);
        //4、请求转发到/pages/manager/book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);

    }
}
